package com.darren.befriend.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.darren.befriend.R;

public class GenderSegment extends LinearLayout implements OnClickListener {

	private Context mContext;
	private TextView mLeft;
	private TextView mRight;
	private boolean mLeftSelected;
	private OnGenderChangeListener mListener;
	
	public GenderSegment(Context context) {
		super(context);
		mContext = context;
		initViews();
	}
	
	public GenderSegment(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initViews();
	}
	
	private void initViews() {
		LayoutInflater.from(getContext()).inflate(R.layout.gender_segment_layout, this, true);
		mLeft = (TextView) findViewById(R.id.left);
		mLeft.setOnClickListener(this);
		mRight = (TextView) findViewById(R.id.right);
		mRight.setOnClickListener(this);
	}
	
	public void setOnGenderListener(OnGenderChangeListener listener) {
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left:
			setLeftSelected(true);
			break;
		case R.id.right:
			setLeftSelected(false);
			break;

		default:
			break;
		}
	}		
	/**
	 * left selected or right
	 * @return
	 */
	public boolean getLeftSelected() {
		return mLeftSelected;
	}
	/**
	 * set left selected and right not
	 * @param selected
	 */
	public void setLeftSelected(boolean selected) {
		mLeftSelected = selected;
		if (mListener != null) {
			mListener.onChange(mLeftSelected);
		}
		if (selected) {			
			mLeft.setTextColor(Color.WHITE);
			mLeft.setSelected(true);
			mRight.setTextColor(mContext.getResources().getColor(R.color.app_background_color));
			mRight.setSelected(false);
		} else {
			mRight.setTextColor(Color.WHITE);
			mRight.setSelected(true);
			mLeft.setTextColor(mContext.getResources().getColor(R.color.app_background_color));
			mLeft.setSelected(false);
		}
	}
	
	public interface OnGenderChangeListener {
		public void onChange(boolean isLeftSelected);
	}

}
